Download course materials from here.
bit.ly/———–
# https://bookdown.org/ndphillips/YaRrr/pirateplot.html
library(tidyverse)
install.packages("ggbeeswarm")
library(ggbeeswarm)
Plot boxplots and violin plots for the ratings. Subset by participant.
data %>%
filter(region == 1) %>%
ggplot(aes(x=gram, y=rating, fill=freq)) +
geom_boxplot(position = position_dodge(.9)) +
#geom_violin(alpha=.5) +
facet_wrap(~subj)
Group by region, word, frequency, and grammaticality. Summarise mean and standard error.
data %>%
group_by(region, word, freq, gram) %>%
summarise(meanRT = mean(rt),
seRT = sd(rt)/sqrt(n())) %>%
ggplot(aes(x=region,y=meanRT,colour=gram)) +
geom_point() +
geom_path(aes(lty=freq)) +
geom_errorbar(aes(ymin=meanRT-seRT, ymax=meanRT+seRT),width=.1) +
scale_x_continuous(breaks=c(1:5),labels = c("the","old","VERB","the","boat")) +
ylab("reading time (miliseconds)")
Well, check out the Datasaurus Dozen, which all have the same x/y means and standard deviations:
Dataset
iris:
The function ggplot layers different geometries and aesthetics to build up a plot:
iris %>%
mutate(Sepal.Width = Sepal.Width+rnorm(length(Sepal.Width),0,.1))%>%
ggplot(aes(x=Species,y=Sepal.Width,fill=Species)) +
geom_violin(lty=0,alpha=.5)+
geom_boxplot(alpha=0.5,lwd=.5) +
geom_quasirandom(dodge.width=1, alpha=.5)
In what ways might we change this plot?
iris %>%
mutate(
Sepal.Width = Sepal.Width +
rnorm(
length(Sepal.Width) , 0 , .1
)
) %>%
ggplot(aes(x=Species,y=Sepal.Width)) +
geom_boxplot() +
geom_quasirandom(aes(pch=Species), alpha=.5)
#ggplot(aes(x=Species,y=Sepal.Width,fill=Species)) +
#geom_violin(lty=0,alpha=.5)+
#geom_boxplot(alpha=0.5,lwd=.5) +
#geom_quasirandom(dodge.width=1, alpha=.5)
Going back to quakes, let’s pipe our table into a ggplot (and fill in missing values):
quakes %>%
group_by(mag) %>%
summarise(n=n(),
sta=mean(stations),
staSD=sd(stations),
staSE=staSD/sqrt(n),
dep=mean(depth),
depSD=sd(depth),
depSE=depSD/sqrt(n)) %>%
ggplot(aes(x=mag)) +
geom_point(aes(y=sta),colour="red")+
geom_path(aes(y=sta),colour="red")+
geom_ribbon(aes(ymin=sta-staSD,ymax=sta+staSD),fill="red",alpha=.2) +
geom_ribbon(aes(ymin=sta-staSE,ymax=sta+staSE),fill="red",alpha=.5) +
geom_point(aes(y=dep),colour="blue")+
geom_path(aes(y=dep),colour="blue")+
geom_ribbon(aes(ymin=dep-depSD,ymax=dep+depSD),fill="blue",alpha=.2) +
geom_ribbon(aes(ymin=dep-depSE,ymax=dep+depSE),fill="blue",alpha=.5) +
theme_bw() + ylab("depth OR number of stations reporting")
Inheritance:
ggplot(data=quakes, aes(x=long)) +
geom_point(aes(y=lat,colour=stations)) #+
#geom_path()